package com.heima.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Author Vsunks.v
 * @Blog blog.sunxiaowei.net/996.mba
 * @Description: 自定义元数据对象处理器，处理新增/修改时时间字段和操作人字段
 */
@Component
@Slf4j
public class TimeAndOpMetaObjectHandler implements MetaObjectHandler {

    /**
     * 新增操作时，自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充 [ insert ]...");

        metaObject.setValue("createTime", LocalDateTime.now());

        metaObject.setValue("updateTime", LocalDateTime.now());
    }
    /**
     * 更新操作时，自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {

        log.info("公共字段自动填充 [ update ]...");

        metaObject.setValue("updateTime", LocalDateTime.now());

    }
}